package aiku.numericalcontroloa.Service.Impl;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.outOfManagementMapper;
import aiku.numericalcontroloa.Mapper.FinanceManagement.AccountMapper;
import aiku.numericalcontroloa.Model.Dto.outOfManagementDto;
import aiku.numericalcontroloa.Model.Entity.outOfManagement;
import aiku.numericalcontroloa.Model.Entity.FinanceManagement.Account;
import aiku.numericalcontroloa.Service.outOfManagementService;

@Service
public class outOfManagementServiceImp implements outOfManagementService {

    @Autowired
    private outOfManagementMapper outOfManagementMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Result selectOutOfManagement(outOfManagementDto data) {
        if (data.getPageSize() == null || data.getCurrentPage() == null) {
            return Result.error("分页参数为空");
        }
        LambdaQueryWrapper<outOfManagement> queryWrapper = new LambdaQueryWrapper<>();
        if (data.getTimeDate() != null) {
            LocalDate starDate = LocalDate.of(data.getTimeDate().getYear(), data.getTimeDate().getMonth(), 1);
            LocalDate endDate = starDate.plusMonths(1).minusDays(1);
            queryWrapper.between(outOfManagement::getCreateTime, starDate, endDate);
        }
        if (data.getReceivingAccount() != null) {
            queryWrapper.eq(outOfManagement::getReceivingAccount, data.getReceivingAccount());
        }
        Page<outOfManagement> page = new Page<>(data.getCurrentPage(), data.getPageSize());
        List<outOfManagement> list = outOfManagementMapper.selectPage(page, queryWrapper).getRecords();
        for (outOfManagement value : list) {
            value.setReceivingAccount(accountMapper
                    .selectById(value.getReceivingAccount())
                    .getAccount());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("data", list);
        return Result.success(map);
    }

    @SuppressWarnings("null")
    @Override
    public Result addOutOfManagement(outOfManagementDto data) {
        if (data == null || data.getTotalAmount() == null || data.getAmountPayout() == null
                || data.getAmountPaid() == null || data.getReceivingAccount() == null) {
            return Result.error("参数为空");
        }
        if (accountMapper
                .selectCount(new LambdaQueryWrapper<Account>().eq(Account::getId, data.getReceivingAccount())) < 1) {
            return Result.error("账户不存在");
        }
        outOfManagement Data = new outOfManagement();
        BeanUtils.copyProperties(data, Data);
        Data.setDeleted(0);
        if (outOfManagementMapper.insert(Data) != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

}
